/*
 * @Author: Smthie Smthie@qq.com
 * @Date: 2024-05-13 10:52:37
 * @LastEditors: Smthie Smthie@qq.com
 * @LastEditTime: 2024-05-20 14:38:28
 * @FilePath: \ProtocolTool\src\renderer\src\router\index.ts
 */
import { createRouter, createWebHashHistory } from 'vue-router'
import { useSocketStoreHook } from '@/store/modules/websocket'

// 常量路由
const constRoutes = [
  {
    path: '/',
    name: 'layout',
    component: () => import('@/layout/index.vue'),
    redirect: '/devices',
    children: [
      {
        path: '/devices',
        name: 'Devices',
        component: () => import('@/views/Devices/index.vue'),
        meta: {
          menu: 'Devices'
        }
      },
      {
        path: '/one-click',
        name: 'OneClick',
        component: () => import('@/views/OneClick/index.vue'),
        meta: {
          menu: 'OneClick'
        }
      },
      {
        path: '/manual',
        name: 'Manual',
        component: () => import('@/views/Manual/index.vue'),
        meta: {
          menu: 'Manual'
        }
      }
    ]
  },
  {
    path: '/test/:id',
    name: 'Test',
    component: () => import('@/views/OneClick/RecordDetail/index.vue'),
    meta: {
      menu: 'OneClick'
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes: constRoutes
})

router.beforeEach(async () => {
  if (useSocketStoreHook().stompClient == null) {
    try {
      await useSocketStoreHook().initSocket()
    } catch (err) {
      console.log(err)
    }
  }
  //             next();
  //         } else {
  //             // 获取用户信息
  //             try {
  //                 useAclStoreHook().getUserInfo();

  //                 let accessRoutes = [];
  //                 accessRoutes = await useRouteStoreHook().setAllRoutes();
  //                 accessRoutes.forEach((item) => {
  //                     router.addRoute(item);
  //                 });
  //                 router.addRoute({
  //                     path: '/:pathMatch(.*)',
  //                     redirect: '/404'
  //                 });
  //                 next({ ...to, replace: true });
  //             } catch {
  //                 await useUserStoreHook().resetAll();
  //                 if (recordRoute)
  //                     next({
  //                         path: "/login",
  //                         query: {
  //                             redirect: to.path,
  //                         },
  //                         replace: true,
  //                     });
  //                 else
  //                     next({
  //                         path: "/login",
  //                         replace: true,
  //                     });
  //             }
  //         }
  //     }
  // } else {
  //     if (routesWhiteList.indexOf(to.path) !== -1) {
  //         next();
  //     } else {
  //         if (recordRoute)
  //             next({
  //                 path: "/login",
  //                 query: {
  //                     redirect: to.path,
  //                 },
  //                 replace: true,
  //             });
  //         else
  //             next({
  //                 path: "/login",
  //                 replace: true,
  //             });
  //     }
  // }
})

export default router
